/*
 Copyright (C) 2008  Maurizio Monticelli

 This program is free software; you can redistribute it and/or modify it under the terms of the 
 GNU General Public License as published by the Free Software Foundation; either version 2 of the 
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 Public License for more details.

 You should have received a copy of the GNU General Public License along with this program; if not, write 
 to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 
*/


package ch.justdoit.andriodrpncalc.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.ContentObserver;
import android.database.DataSetObserver;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;;

public class IStackListAdapter implements ListAdapter {

	

    /** Remember our context so we can use it when constructing views. */
    private Context mContext;

    private List<IStackItem> mItems = new ArrayList<IStackItem>();

    public IStackListAdapter(Context context) {
         mContext = context;
    }

    public void addItem(IStackItem it) { mItems.add(it); }

    public void setListItems(List<IStackItem> lit) { mItems = lit; }

    /** @return The number of items in the */
    public int getCount() { return mItems.size(); }

    public IStackItem getItem(int position) { return mItems.get(position); }

    public boolean areAllItemsSelectable() { return false; }

    public boolean isSelectable(int position) {
         try{
              return mItems.get(position).isSelectable();
         }catch (IndexOutOfBoundsException aioobe){
              return true; 
         }
    }

    /** Use the array index as a unique id. */
    public long getItemId(int position) {
         return position;
    }

    /** @param convertView The old view to overwrite, if one is passed
     * @returns a IconifiedTextView that holds wraps around an IconifiedText */
    public View getView(int position, View convertView, ViewGroup parent) {
         IStackItemView btv;
         if (convertView == null) {
              btv = new IStackItemView(mContext, mItems.get(position));
         } else { // Reuse/Overwrite the View passed
              // We are assuming(!) that it is castable!
              btv = (IStackItemView) convertView;
              btv.setText(mItems.get(position).getText());
              btv.setIconText(mItems.get(position).getIconText());
         }
         return btv;
    }

	@Override
	public int getNewSelectionForKey(int currentSelection, int keyCode,
			KeyEvent event) {
		// TODO Auto-generated method stub
		return 0;
	}

//	@Override
//	public void registerContentObserver(ContentObserver observer) {
//		// TODO Auto-generated method stub
//		
//	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean stableIds() {
		// TODO Auto-generated method stub
		return false;
	}

//	@Override
//	public void unregisterContentObserver(ContentObserver observer) {
//		// TODO Auto-generated method stub
//		
//	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver arg0) {
		// TODO Auto-generated method stub
		
	}

}
